package com.ecnu.web.controller.page;

import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.common.utils.OtherUtil;
import com.ecnu.system.domain.dto.incubation.FactoryPageDTO;
import com.ecnu.system.domain.dto.page.PageDTO;
import com.ecnu.system.service.IIncubationFactoryService;
import com.ecnu.system.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * ecnu_二期_首页 前端控制器
 * </p>
 *
 * @author wcy
 * @since 2025-10-21
 */
@RestController
@Api(tags = "二期_首页")
@RequestMapping("/page")
@Validated//用来校验controller参数带@notblack类似的注解
public class PageController extends BaseController {

    @Autowired
    private IIncubationFactoryService incubationFactoryService;

    @Autowired
    private IProjectService projectService;


    private static LoginUser getProUser() {
        LoginUser loginUser = new LoginUser();
        //看情况是否需要优化--获取登录信息问题
        try {
            loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {

        }
        return loginUser;
    }

    @ApiOperation(value = "首页-科技成果转化车间列表")
    @PostMapping("/gteFactoryList")
    public TableDataInfo gteFactoryList(@RequestBody PageDTO pageDTO) {
        try {
            //LoginUser proUser = getProUser();
            LoginUser proUser = null;
            FactoryPageDTO dto = new FactoryPageDTO();
            dto.setPageDomain(pageDTO.getPageDomain());
            dto.setStatus("2");
            return incubationFactoryService.listForPageIncubation(dto, proUser);
        } catch (Exception e) {
            logger.error("获取科技成果转化车间列表错误：", e.getMessage());
            return OtherUtil.tableError("获取科技成果转化车间列表错误，请联系管理员");
        }
    }

    @ApiOperation(value = "首页成果信息")
    @PostMapping("/getResultInfo")
    public AjaxResult getResultInfo() {
        try {
            //LoginUser loginUser = getLoginUser();
            LoginUser loginUser = null;
            return AjaxResult.success(incubationFactoryService.getAllIncubationFactoriesStatistics(loginUser));
        } catch (Exception e) {
            logger.error("获取首页成果信息：", e.getMessage());
            return AjaxResult.error("获取首页成果信息错误，请联系管理员");
        }

    }

    @ApiOperation(value = "最热实验")
    @PostMapping("/getHotProjects")
    public TableDataInfo getHotProjects(@RequestBody PageDTO pageDTO) {
        try {
            return projectService.getHotProjects(pageDTO);
        } catch (Exception e) {
            logger.error("获取最热实验错误：", e.getMessage());
            return OtherUtil.tableError("获取最热实验错误，请联系管理员");
        }
    }

    @ApiOperation(value = "最新实验")
    @PostMapping("/getLatestProjects")
    public TableDataInfo getLatestProjects(@RequestBody PageDTO pageDTO) {
        try {
            return projectService.getLatestProjects(pageDTO);
        } catch (Exception e) {
            logger.error("获取最新实验错误：", e.getMessage());
            return OtherUtil.tableError("获取最新实验错误，请联系管理员");
        }
    }

    @ApiOperation(value = "推荐实验")
    @PostMapping("/getRecommendedProjects")
    public TableDataInfo getRecommendedProjects(@RequestBody PageDTO pageDTO) {
        try {
            return projectService.getRecommendedProjects(pageDTO);
        } catch (Exception e) {
            logger.error("获取推荐实验错误：", e.getMessage());
            return OtherUtil.tableError("获取推荐实验错误，请联系管理员");
        }
    }


}
